Ext.define('console.mappingWindow', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'console.store.deptStore',
        'console.store.deptGroupStore',
        'Ext.util.Format',
        'Ext.grid.Panel',
        'Ext.grid.RowNumberer'
    ],

    id: 'mappingWindow',
    init: function () {
        this.launcher = {
            text: '部门分组',
            iconCls: 'icon-grid'
        };
    },

    createWindow: function () {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('mappingWindow');
        if (!win) {
            win = desktop.createWindow({
                id: 'mappingWindow',
                title: '部门分组',
                width: 1124,
                height: 620,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: "border",
                items: [
                    {
                        border: false,
                        xtype: 'grid',
                        region: 'west',
                        width: '30%',
                        split: true,
                        itemId: 'deptGroup',
                        store: Ext.createWidget('deptGroupStore'),
                        columns: [
                            {
                                text: "代码",
                                sortable: true,
                                dataIndex: 'groupId'
                            },
                            {
                                text: "组名",
                                sortable: true,
                                flex: 1,
                                dataIndex: 'name'
                            }
                        ],
                        listeners: {
                            select: function (t, r, i, o) {
                                var questions = this.ownerCt.child("#depts");
                                questions.getStore().load({params: {groupId: r.data.groupId}});
                            }
                        },
                        tbar: [{
                            text: '添加',
                            tooltip: '添加一行',
                            iconCls: 'add',
                            handler: this.addGroup
                        }, '-', {
                            text: '修改',
                            tooltip: '修改操作',
                            iconCls: 'option',
                            handler: this.optionGroup
                        }, '-', {
                            text: '删除',
                            tooltip: '删除一组数据',
                            iconCls: 'remove',
                            handler: this.removeGroup
                        }, '-', {
                            text: '刷新',
                            tooltip: '刷新数据',
                            iconCls: 'refresh',
                            handler: this.refreshGroup
                        }]
                    },
                    {
                        border: false,
                        xtype: 'grid',
                        region: 'center',
                        itemId: 'depts',
                        split: true,
                        store: Ext.createWidget('deptStore', {autoLoad: false}),
                        columns: [
                            new Ext.grid.RowNumberer(),
                            {
                                text: "代码",
                                width: '10%',
                                sortable: true,
                                dataIndex: 'code'
                            },
                            {
                                text: "部门/科室名称",
                                width: '30%',
                                sortable: true,
                                dataIndex: 'name'
                            },
                            {
                                text: "上级科室代码",
                                width: '20%',
                                sortable: true,
                                dataIndex: 'pcode'
                            },
                            {
                                text: "科室类型",
                                width: '20%',
                                sortable: true,
                                dataIndex: 'type'
                            },
                            {
                                text: "状态",
                                width: '20%',
                                sortable: true,
                                dataIndex: 'status',
                                renderer: function (val) {
                                    if (val) {
                                        return "有效"
                                    }
                                    else {
                                        return '无效'
                                    }
                                }
                            }
                        ],
                        tbar: [{
                            text: '添加部门',
                            tooltip: '分组中添加部门',
                            iconCls: 'add',
                            handler: this.add
                        }, '-', {
                            text: '删除部门',
                            tooltip: '删除一组数据',
                            iconCls: 'remove',
                            handler: this.remove
                        }]
                    }
                ]
            });
        }
        return win;
    },


    addGroup: function () {
        Ext.create('Ext.window.Window', {
            modal: true,
            items: [{
                xtype: 'form',
                defaults: {
                    labelAlign: 'left',
                    margin: '10 10 10 10',
                    width: '90%',
                    xtype: 'textfield'
                },
                items: [{
                    name: 'groupId',
                    maxLength: 12,
                    fieldLabel: '代码',
                    allowBlank: false
                }, {
                    name: 'name',
                    fieldLabel: '组名'
                }, {
                    name: 'describe',
                    fieldLabel: '备注'
                }],
                buttons: [{
                    text: '重置',
                    handler: function () {
                        this.up('form').getForm().reset();
                    }
                }, {
                    text: '提交',
                    handler: function () {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                url: 'dept/addGroup',
                                success: function (form, action) {
                                    Ext.Msg.alert('', '操作成功', function () {
                                        form.owner.ownerCt.close();
                                        Ext.getCmp('mappingWindow').child("#deptGroup").store.reload();
                                    }, this);
                                },
                                failure: function (form, action) {
                                    Ext.Msg.alert('', '操作失败');
                                }
                            });
                        }
                    }
                }]
            }]
        }).show();
    },

    optionGroup: function () {
        var row = Ext.getCmp('mappingWindow').child("#deptGroup").getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行记录');
            return;
        }
        var data = row[0].data;
        var win = Ext.create('Ext.window.Window', {
            modal: true,
            items: [{
                xtype: 'form',
                defaults: {
                    labelAlign: 'left',
                    margin: '10 10 10 10',
                    width: '90%',
                    xtype: 'textfield'
                },
                items: [{
                    name: 'groupId',
                    fieldLabel: '代码',
                    allowBlank: false,
                    readOnly: true
                }, {
                    name: 'name',
                    fieldLabel: '组名',
                    allowBlank: false
                }, {
                    name: 'describe',
                    fieldLabel: '备注'
                }],
                buttons: [{
                    text: '重置',
                    handler: function () {
                        this.up('form').getForm().reset();
                    }
                }, {
                    text: '提交',
                    handler: function () {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                url: 'dept/updateGroup',
                                success: function (form, action) {
                                    Ext.Msg.alert('', '修改成功', function () {
                                        form.owner.ownerCt.close();
                                        Ext.getCmp('mappingWindow').child("#deptGroup").getStore().reload();
                                    }, this);
                                },
                                failure: function (form, action) {
                                    Ext.Msg.alert('', '操作失败');
                                }
                            });
                        }
                    }
                }]
            }]
        });
        win.query('form')[0].getForm().setValues(data);
        win.show();
    },

    removeGroup: function () {
        var row = Ext.getCmp('mappingWindow').child("#deptGroup").getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行记录');
            return;
        }
        var groupId = row[0].data.groupId;
        Ext.MessageBox.confirm('', '您确定要删除吗?', function (opt) {
            if (opt == 'yes') {
                Ext.Ajax.request({
                    url: 'dept/deleteGroup',
                    params: {groupId: groupId},
                    success: function (response) {
                        var res = Ext.decode(response.responseText);
                        if (res.success) {
                            Ext.Msg.alert('删除成功', '删除成功', function () {
                                Ext.getCmp('mappingWindow').child("#deptGroup").store.reload();
                            }, this);
                        } else {
                            Ext.Msg.alert('操作失败', res.failure);
                        }
                    },
                    failure: function (action) {
                        Ext.Msg.alert('', '操作信息失败');
                    }
                });
            }
        })
    },
    refreshGroup: function () {
        Ext.getCmp('mappingWindow').child("#deptGroup").store.reload();
    },

    add: function () {
        var row = Ext.getCmp('mappingWindow').child("#deptGroup").getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行分组');
            return;
        }
        Ext.create('Ext.window.Window', {
            modal: true,
            title: '问题列表',
            items: [{
                border: false,
                xtype: 'grid',
                width: 1024,
                height: 600,
                multiSelect: true,
                itemId: 'selectDepts',
                selModel: Ext.create('Ext.selection.CheckboxModel'),
                store: Ext.createWidget('deptStore', {
                    listeners: {
                        beforeload: function (store, options) {
                            var extraParams = {groupId: row[0].data.groupId, filter: 'filter'};
                            Ext.apply(store.proxy.extraParams, extraParams);
                        }
                    }
                }),
                columns: [
                    new Ext.grid.RowNumberer(),
                    {
                        text: "代码",
                        width: '10%',
                        sortable: true,
                        dataIndex: 'code'
                    },
                    {
                        text: "部门/科室名称",
                        width: '30%',
                        sortable: true,
                        dataIndex: 'name'
                    },
                    {
                        text: "上级科室代码",
                        width: '20%',
                        sortable: true,
                        dataIndex: 'pcode'
                    },
                    {
                        text: "科室类型",
                        width: '20%',
                        sortable: true,
                        dataIndex: 'type'
                    },
                    {
                        text: "状态",
                        width: '20%',
                        sortable: true,
                        dataIndex: 'status',
                        renderer: function (val) {
                            if (val) {
                                return "有效"
                            }
                            else {
                                return '无效'
                            }
                        }
                    }
                ],
                tbar: [{
                    text: '确认添加',
                    tooltip: '确认添加',
                    iconCls: 'add',
                    handler: function () {
                        var subGrid = this.ownerCt.ownerCt;
                        var subRow = subGrid.getSelectionModel().selected.items;
                        if (subRow.length == 0) {
                            Ext.Msg.alert('', '需要选中一行记录');
                            return;
                        } else {

                            var deptCodes = new Array();
                            for (var i = 0; i < subRow.length; i++) {
                                deptCodes.push(subRow[i].data.code);
                            }
                            Ext.Ajax.request({
                                url: 'dept/addDeptToGroup',
                                params: {deptCode: deptCodes, groupId: row[0].data.groupId},
                                success: function (response) {
                                    var res = Ext.decode(response.responseText);
                                    if (res.success) {
                                        Ext.Msg.alert('提示', '添加成功', function () {
                                            subGrid.getStore().reload();
                                            Ext.getCmp('mappingWindow').child("#depts").getStore().load({params: {groupId: row[0].data.groupId}});
                                        }, this);
                                    } else {
                                        Ext.Msg.alert('操作失败', res.failure);
                                    }
                                },
                                failure: function (action) {
                                    Ext.Msg.alert('', '操作信息失败');
                                }
                            });
                        }
                    }
                }]
            }]
        }).show();
    },


    remove: function () {
        var row = Ext.getCmp('mappingWindow').child("#deptGroup").getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行分组');
            return;
        }
        var subRow = Ext.getCmp('mappingWindow').child("#depts").getSelectionModel().selected.items;
        if (subRow.length == 0) {
            Ext.Msg.alert('', '需要选中一行问题');
            return;
        }
        var deptCode = subRow[0].data.code;
        var groupId = row[0].data.groupId;
        Ext.MessageBox.confirm('', '您确定要删除吗?', function (opt) {
            if (opt == 'yes') {
                Ext.Ajax.request({
                    url: 'dept/deleteDeptFromGroup',
                    params: {deptCode: deptCode, groupId: groupId},
                    success: function (response) {
                        var res = Ext.decode(response.responseText);
                        if (res.success) {
                            Ext.Msg.alert('删除成功', '删除成功', function () {
                                Ext.getCmp('mappingWindow').child("#depts").getStore().load({params: {groupId: row[0].data.groupId}});
                            }, this);
                        } else {
                            Ext.Msg.alert('操作失败', res.failure);
                        }
                    },
                    failure: function (action) {
                        Ext.Msg.alert('', '操作信息失败');
                    }
                });
            }
        })
    }
});

